//
// Created by Administrator on 2021/10/16.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    vector<int> sortByBits(vector<int> &arr) {
        sort(arr.begin(), arr.end(), [&](int a, int b) {
            int oneNumA = __builtin_popcount(a),oneNumB = __builtin_popcount(b);
            if (oneNumA==oneNumB)
                return a<b;
            else{
                return oneNumA < oneNumB;
            }
        });
        return arr;
    }
};

int main() {
    vector<int> array{1,624,377,1107,4401,8641,8742,1111,3554,5311,6631,6978,7694,9193,7403,7644};
    Solution solution;
    vector<int> ans = solution.sortByBits(array);
    for (auto &x: ans) cout << x << endl;
    return 0;
}
